package com.team.weigou.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.team.weigou.R;
import com.team.weigou.fragment.HomeFragment;
import com.team.weigou.fragment.HotFragment;
import com.team.weigou.fragment.MyFragment;
import com.team.weigou.fragment.RebateFragment;
import com.team.weigou.utils.FragmentTabUtils;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.activity_main_layout)
public class MainActivity extends AppCompatActivity implements FragmentTabUtils.OnRgsExtraCheckedChangedListener {
    @ViewInject(R.id.main_rgs)
    private RadioGroup rgs;
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        getSupportActionBar().hide();
        initView();
    }

    private void initView() {
        fragments.add(new HomeFragment());
        fragments.add(new HotFragment());
        fragments.add(new RebateFragment());
        fragments.add(new MyFragment());
        FragmentTabUtils tabUtils = new FragmentTabUtils(getSupportFragmentManager(),
                fragments, R.id.fragment_home_container, rgs);
        tabUtils.setOnRgsExtraCheckedChangedListener(this);
    }

    /**
     * 第一次按下去的时间
     */
    private long firstTime;

    /**
     * 系统返回键监听
     */
    @Override
    public void onBackPressed() {
        long secondTime = System.currentTimeMillis();
        if (secondTime - firstTime > 2000) {//如果两次按键时间间隔大于2秒，则不退出
            Toast.makeText(this, "再按一次退出微店购物", Toast.LENGTH_SHORT).show();
            firstTime = secondTime;//更新firstTime
            return;
        } else {
            //两次按键小于2秒时，退出应用
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }

    /**
     * tab变化监听类
     *
     * @param radioGroup
     * @param checkedId
     * @param index
     */
    @Override
    public void OnRgsExtraCheckedChanged(RadioGroup radioGroup, int checkedId, int index) {

    }
}
